package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.R;
import com.mexico.inloancash.bean.BaseBean;
import com.mexico.inloancash.bean.India_NewBank_ListBean;
import com.mexico.inloancash.dialog.mIndiaChooseDialog;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.india_interfacedai.IndiaChooseAddressInterface;
import com.mexico.inloancash.utils.LogTrack;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 添加银行卡
 */
public class IndiaAddBankActivity extends BaseActivity implements IndiaChooseAddressInterface {

    @BindView(R.id.logon_back)
    ImageView mIndiaLogonBack;

    @BindView(R.id.title)
    TextView mTitle;

    @BindView(R.id.user_name)
    EditText mUserName;

    @BindView(R.id.phone_number)
    EditText mIndiaPhoneNumber;

    @BindView(R.id.bank_card)
    EditText mIndiaBankCard;

    @BindView(R.id.bank_name)
    TextView mIndiaBankName;

    @BindView(R.id.bank_linear)
    LinearLayout mIndiaBankLinear;

    @BindView(R.id.commit_button)
    Button mIndiaCommitButton;

    @BindView(R.id.bank_code_ifsc)
    EditText mIndiaEditTextIFSCCode;


    private mIndiaChooseDialog mIndiaDialog;
    private List<String> mIndiaBankNameList;
    private List<String> mIndiaBankCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_view_addnewbank);
        ButterKnife.bind(this);
        mTitle.setText("Add bank card");
        mIndiaBankNameList = new ArrayList<>();
        mIndiaBankCode = new ArrayList<>();
        isIndiaEmpty();
        getIndiaBank();//获取银行列表接口
    }

    @OnClick({R.id.logon_back, R.id.commit_button, R.id.bank_linear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.commit_button:
                if(TextUtils.isEmpty(mIndiaBankCard.getText().toString().trim())){
                    Toast.makeText(IndiaAddBankActivity.this,"Bank card number cannot be empty",Toast.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(mIndiaBankName.getText().toString().trim())){
                    Toast.makeText(IndiaAddBankActivity.this,"Bank name cannot be empty",Toast.LENGTH_SHORT).show();
                    return;
                }

                //银行卡的IFSC代码
                if(TextUtils.isEmpty(mIndiaEditTextIFSCCode.getText().toString().trim())){
                    Toast.makeText(IndiaAddBankActivity.this,"IFSC cannot be empty",Toast.LENGTH_SHORT).show();
                    return;
                }
                saveIndiaBank();
                break;
            case R.id.bank_linear:
                if(mIndiaBankNameList.size()>0){
                    mIndiaDialog = new mIndiaChooseDialog(this, this, "Bank", mIndiaBankNameList, "bank");
                    mIndiaDialog.show();
                }
                break;
        }
    }

    public void saveIndiaBank() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> saveBank = api.saveBank(
                MyPreferencesManager.getString("token", ""),
                MyApplication.source,
                mIndiaBankCard.getText().toString().trim(),
                mIndiaBankCode.get(mIndiaBankNameList.indexOf(mIndiaBankName.getText().toString().trim())),
                mIndiaEditTextIFSCCode.getText().toString().trim()
        );
        saveBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                    if (baseBean.getCode() == 200) {
                        MyToast.show(IndiaAddBankActivity.this, "Added successfully");
                        setResult(0);
                        finish();
                    } else if (baseBean.getCode() == 401) {
                        MyPreferencesManager.putString("token", "");
                        MyPreferencesManager.putLogin(false);
                        MainActivity.isShow=false;
                        Intent intent = new Intent(IndiaAddBankActivity.this, IndiaLoginActivity.class);
                        startActivity(intent);
                    } else if (baseBean.getCode() == 60) {

                    } else {
                        MyToast.show(IndiaAddBankActivity.this, baseBean.getMsg());
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }


    public void getIndiaBank() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> saveBank = api.getBankInfo();
        saveBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    India_NewBank_ListBean baseBean = new Gson().fromJson(response.body(), India_NewBank_ListBean.class);
                    if (Integer.parseInt(baseBean.getCode()) == 200) {
                        List<India_NewBank_ListBean.DataBean> dataBeanList=baseBean.getData();
                        if(dataBeanList.size()>0){
                            mIndiaBankNameList.clear();
                            mIndiaBankCode.clear();
                            for (int i = 0; i < dataBeanList.size(); i++) {
                                India_NewBank_ListBean.DataBean bean=dataBeanList.get(i);
                                mIndiaBankNameList.add(bean.getSerpay());
                                mIndiaBankCode.add(bean.getCode());
                            }
                        }
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }


    //判断内容是否有改变
    public void isIndiaEmpty() {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }



    @Override
    public void choose(String type, String string) {
        if (type.equals("bank")) {
            mIndiaBankName.setText(string);
        }
    }
}
